package com.mjw.zstu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mjw.zstu.entity.Company;
import com.mjw.zstu.entity.Exhibition;
import com.mjw.zstu.entity.Exhibitor;
import com.mjw.zstu.mapper.CompanyMapper;
import com.mjw.zstu.mapper.ExhibitonMapper;
import com.mjw.zstu.mapper.ExhibitorMapper;
import com.mjw.zstu.resultDto.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ExhibitorService {
    @Autowired
    ExhibitorMapper exhibitorMapper;
    @Autowired
    ExhibitonMapper exhibitonMapper;
    @Autowired
    CompanyMapper companyMapper;
    @Transactional
    public JsonResult join(Exhibitor exhibitor){
        Exhibitor exhibitor1 =exhibitorMapper.selectOne(new QueryWrapper<>(exhibitor));
//        int company_id=exhibitor1.getCompany_id();
        Company company =companyMapper.selectById(exhibitor.getCompany_id());
        if(company.getIs_review()==0){
            return new JsonResult(500,"未通过审核的公司不能参加会展",null);
        }
        if(exhibitor1!=null){
            return new JsonResult(500,"已经参加过此会展啦",null);
        }
        Exhibition exhibition1=exhibitonMapper.selectById(exhibitor.getExhibition_id());
//        List<Map> l =exhibitorMapper.hasplace(exhibitor.getExhibition_id());
        if(exhibition1.getLast_place()==0){
            return new JsonResult(500,"场馆满啦",null);
        }
        System.out.println(exhibitor);
        exhibitorMapper.insert(exhibitor);
        Exhibition exhibition=new Exhibition();
        exhibition.setId(exhibitor.getExhibition_id());
        exhibition.setLast_place(exhibition1.getLast_place()-1);
        exhibitonMapper.updateById(exhibition);
        return new JsonResult(200,"参加成功",null);
    };

}
